package com.hlf.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * ID生成器,生成64位递增序列<br>
 * From: https://github.com/twitter/snowflake <br>
 * An object that generates IDs. This is broken into a separate class in case we
 * ever want to support multiple worker threads per process
 * 
 * @author shuliangxing
 * 
 * @date 2015-8-25下午5:55:38
 */
public class IdWorker {

    protected static final Logger LOG = LoggerFactory.getLogger(IdWorker.class);

    // 机器标识
    private long workerId;

    // 数据中心标识
    private long datacenterId;

    // 序列
    private long sequence = 0L;

    // 某一时刻时间戳, 2017-01-01 00:00:00
    private long twepoch = 1483200000000L;

    // 机器标识位数
    private long workerIdBits = 5L;

    // 数据中心标识位数
    private long datacenterIdBits = 5L;

    // 机器ID最大值
    private long maxWorkerId = -1L ^ (-1L << workerIdBits);

    // 数据中心ID最大值
    private long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);

    // 毫秒内自增位
    private long sequenceBits = 12L;

    // 机器ID偏左移12位
    private long workerIdShift = sequenceBits;

    // 数据中心ID左移17位
    private long datacenterIdShift = sequenceBits + workerIdBits;

    // 时间毫秒左移22位
    private long timestampLeftShift = sequenceBits + workerIdBits
            + datacenterIdBits;

    private long sequenceMask = -1L ^ (-1L << sequenceBits);

    private long lastTimestamp = -1L;

    /**
     * 构造器
     *
     * @param workerId
     *            机器标识
     * @param datacenterId
     *            数据中心标识
     */
    public IdWorker(long workerId, long datacenterId) {
        // sanity check for workerId
        if (workerId > maxWorkerId || workerId < 0) {
            throw new IllegalArgumentException(String.format(
                    "worker Id can't be greater than %d or less than 0",
                    maxWorkerId));
        }
        if (datacenterId > maxDatacenterId || datacenterId < 0) {
            throw new IllegalArgumentException(String.format(
                    "datacenter Id can't be greater than %d or less than 0",
                    maxDatacenterId));
        }
        this.workerId = workerId;
        this.datacenterId = datacenterId;
        LOG.info(String
                .format("worker starting. timestamp left shift %d, datacenter id bits %d, worker id bits %d, sequence bits %d, workerid %d",
                        timestampLeftShift, datacenterIdBits, workerIdBits,
                        sequenceBits, workerId));
    }

    public synchronized long nextId() {
        long timestamp = timeGen();

        // 时间错误
        if (timestamp < lastTimestamp) {
            LOG.error(String.format(
                    "clock is moving backwards.  Rejecting requests until %d.",
                    lastTimestamp));
            throw new RuntimeException(
                    String.format(
                            "Clock moved backwards.  Refusing to generate id for %d milliseconds",
                            lastTimestamp - timestamp));
        }

        if (lastTimestamp == timestamp) {
            // 当前毫秒内，则+1
            sequence = (sequence + 1) & sequenceMask;
            // 当前毫秒内计数满了，则等待下一秒
            if (sequence == 0) {
                timestamp = tilNextMillis(lastTimestamp);
            }
        } else {
            sequence = 0L;
        }

        lastTimestamp = timestamp;

        // ID偏移组合生成最终的ID，并返回ID
        return ((timestamp - twepoch) << timestampLeftShift)
                | (datacenterId << datacenterIdShift)
                | (workerId << workerIdShift) | sequence;
    }

    /**
     * 等待下一个毫秒的到来
     *
     * @param lastTimestamp
     * @return
     */
    protected long tilNextMillis(long lastTimestamp) {
        long timestamp = timeGen();
        while (timestamp <= lastTimestamp) {
            timestamp = timeGen();
        }
        return timestamp;
    }

    /**
     * 当前时间戳
     *
     * @return
     */
    protected long timeGen() {
        return System.currentTimeMillis();
    }

}
